package handlers

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"user-perm-mgr/modles"
)

func NAdd(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))
	data := modles.RoleNode{
		Rid: rid,
		Nid: nid,
	}
	modles.DB.Create(&data)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "关联成功",
	})
}

func NList(c *gin.Context) {
	rid := c.Query("rid")
	var data []modles.Node
	/*
		SELECT
		  *
		FROM
		  nodes
		  JOIN role_nodes ON nodes.id = role_nodes.nid
		WHERE
		  role_nodes.rid = 1
	*/
	modles.DB.Table("nodes").
		Joins("JOIN role_nodes ON nodes.id = role_nodes.nid").
		Where("role_nodes.rid = ?", rid).Find(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查找成功",
		"data": data,
	})
}
